package com.zuxia.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zuxia.entity.Depart;
import com.zuxia.service.IDepartService;


@Controller("departAction")
@Scope("prototype")//定义单例或多例
@ParentPackage("struts-default")
@Namespace("/")
@Results({
	@Result(name="success",type="dispatcher",location="/admin/Department_list.jsp"),
	@Result(name="yes",type="redirectAction",location="/selectDepart"),
	@Result(name="no",type="redirectAction",location="/selectDepart")
})
public class DepartAction extends ActionSupport implements ModelDriven<Depart>{

	private static final long serialVersionUID = 1L;
	
	private Depart depart =new Depart();
	
	private List<Depart> departs;
	private int departsSize;

	@Resource(name="departService")
	private IDepartService departService;
	
	public Depart getModel() {
		return depart;
	}
	
	@Action("addDepart")
	public String addDepart(){
		System.out.println(departService.deleteDepart(depart));
		return departService.svaeDepart(depart);
	}
	@Action("selectDepart")
	public String selectDepart(){
		departs =departService.findAll();
		departsSize=departs.size();
		ActionContext.getContext().put("departs", departs);
		return "success";
	}
	
	@Action("updateDepart")
	public String updateDepart() {
		System.out.println(departService.deleteDepart(depart));
		return departService.updateDepart(depart);
	}
	@Action("deleteDepart")
	public String deleteDepart(){
		System.out.println(departService.deleteDepart(depart));
		return departService.deleteDepart(depart);
	}
	
	public List<Depart> getDeparts() {
		return departs;
	}

	public void setDeparts(List<Depart> departs) {
		this.departs = departs;
	}
	
	public int getDepartsSize() {
		return departsSize;
	}

	public void setDepartsSize(int departsSize) {
		this.departsSize = departsSize;
	}
	
}
